package com.dongzili.demo.common.config.async;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()-2);  // 核心线程池大小
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors()-2);   // 最大线程池大小
        executor.setQueueCapacity(100); // 队列容量
        executor.setThreadNamePrefix("async-task-");
        executor.initialize();
        return executor;
    }
}